﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.CommonMvc;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.QianWeb.App_Start;
using ZSZ.QianWeb.Models.House;

namespace ZSZ.QianWeb.Controllers
{
    public class HouseController : Controller
    {
        public IHouseService HouseService { get; set; }
        public IAttachmentService AttachmentService { get; set; }
        public IRegionService RegionService { get; set; }
        public ICityService CityService { get; set; }
        public IHouseAppointmentService HouseAppointmentService { get; set; }
        // GET: House
        public ActionResult Index(long id)
        {
            IndexGetModel model = new IndexGetModel();
            if (HttpContext.Cache.Get("house_" + id) == null)
            {
                var dto = HouseService.GetById(id);
                var attchs = AttachmentService.GetByHouseId(id);
                var pic = HouseService.GetPics(id);
                model.Attachments = attchs;
                model.House = dto;
                model.HousePics = pic;
                HttpContext.Cache.Insert("house_" + dto.Id, model, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero);
            }
            model = (IndexGetModel)HttpContext.Cache.Get("house_" + id);
            return View(model);
        }
        [HttpPost]
        public ActionResult YuYue(long houseId, string name, string phone, DateTime lookDate)
        {
            long? userId = QianHelper.GetUserId(HttpContext);
            long id = HouseAppointmentService.AddNew(userId, name, phone, houseId, lookDate);
            if (id < 1)
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "添加失败" });
            }
            return Json(new JsonCommon { Status = "ok" });
        }
        private static void MonthRentSuan(string monthrent, out int? start, out int? end)
        {
            if (monthrent == null)
            {
                start = null;
                end = null;
                return;
            }
            string[] zu = monthrent.Split('-');
            start = Convert.ToInt32(zu[0]);
            end = Convert.ToInt32(zu[1]);
        }
        public ActionResult Search(long? regionId, string monthRent, string orderBy)
        {
            long? cityId = QianHelper.GetCityId(HttpContext);
            var city = CityService.GetById((long)cityId);
            int? start;
            int? end;
            MonthRentSuan(monthRent, out start, out end);
            HouseSearchOptions search = new HouseSearchOptions();
            search.CityId = (long)cityId;
            search.CurrentIndex = 1;
            search.EndMonthRent = end;
            search.StartMonthRent = start;
            switch (orderBy)
            {
                case "montRentDesc":
                    search.OrderByType = OrderByType.MonthRentDesc;
                    break;
                case "montRentAsc":
                    search.OrderByType = OrderByType.MonthRentAsc;
                    break;
                case "areaDesc":
                    search.OrderByType = OrderByType.AreaDesc;
                    break;
                case "areaAsc":
                    search.OrderByType = OrderByType.AreaAsc;
                    break;
            }
            search.PageSize = 10;
            search.RegionId = regionId;
            search.TypeId = 14;
            HouseSearchResult result = HouseService.Search(search);
            SearchGetModel model = new SearchGetModel();
            model.HouseSearch = result;
            model.Regions = RegionService.GetByCityId((long)cityId);
            ViewBag.CityName = city.Name;
            return View(model);
        }
    }
}